package com.zhanbo.zhanbowymusic.main.featured;

import android.graphics.Color;
import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.ArtistAlbumBean;
import com.mwx.baselib.bean.ArtistSongListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.view.Divider;
import com.mwx.baselib.view.DividerItemDecoration;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerAlbumListBinding;
import com.zhanbo.zhanbowymusic.main.bean.SingerAlbumDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerAlbumListAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 歌手专辑列表
 */
public class SingerAlbumFragment extends BaseFragment<FragmentSingerAlbumListBinding> {
    private SingerAlbumListAdapter adapter;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_singer_album_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        if (getArguments()==null){
            return;
        }
        NetworkManager.getInstance().getApiService().artistAlbumList(getArguments().getInt("id"))
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<ArtistAlbumBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<ArtistAlbumBean> listBaseResponses) {
                        adapter.getItems().addAll(listBaseResponses.getData().getItems());
                        adapter.notifyDataSetChanged();

                    }
                });
    }

    @Override
    protected void initView() {
        DividerItemDecoration dividerItemDecoration= new DividerItemDecoration();
        dividerItemDecoration.setDividerLookup(new DividerItemDecoration.DividerLookup() {
            @Override
            public Divider getVerticalDivider(int position) {
                return new Divider.Builder()
                        .size(20)
                        .color(Color.TRANSPARENT)
                        .build();
            }

            @Override
            public Divider getHorizontalDivider(int position) {
                return new Divider.Builder()
                        .size(20)
                        .color(Color.TRANSPARENT)
                        .build();
            }
        });
        binding.rvSingerAlbumList.addItemDecoration(dividerItemDecoration);
        binding.rvSingerAlbumList.setLayoutManager(new GridLayoutManager(_mActivity, 4));
         adapter = new SingerAlbumListAdapter(_mActivity);

        binding.rvSingerAlbumList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<ArtistAlbumBean.ItemsDTO>() {
            @Override
            public void onItemClick(int position) {
                EventBus.getDefault().post(new SingerAlbumDetailsEBBean(adapter.getItems().get(position)));
            }
        });
    }

    @Override
    protected void initListener() {

    }

    public static SingerAlbumFragment newInstance(int singerId) {

        Bundle args = new Bundle();
        args.putInt("id",singerId);
        SingerAlbumFragment fragment = new SingerAlbumFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
